#include"filehandler.h"
char *pattern = "[A-Za-z][A-Za-z]+";
int charateCount(FILE *fp) 
{
    int num=-1;
    char buf;
    rewind(fp);
    while(!feof(fp))
    {   
        buf=fgetc(fp);
        #ifdef DEBUG
        printf("%c\n",buf);
        sleep(1);
        #endif
        num++;
    }   
    return num;
}

int wordCount(FILE *fp)
{
    int num=0;
    char buf;
    int isWord=0;
    rewind(fp);
    while(!feof(fp))
    {
        buf=fgetc(fp);
        switch(buf)
        {
            case ' ':
            case '<':
            case '>':
            case '{':
            case '}':
            case '#':
            case '-':
            case '+':
            case '=':
            case '/':
            case '*':
            case ';':
            case ',':
            case '&':
            case '^':
            case '$':
            case '!':
            case '@':
            case '%':
            case '\n':
            case '\'':
            case -1:
            case ':':isWord=0;break;
            default:
            {
                if(isWord)break;
                else num++;
                isWord=1;
            }
        }
    }
    return num;
}

int lineCount(FILE *fp)
{
    int num=0;
    char buf;
    rewind(fp);
    while(!feof(fp))
    {
        buf=fgetc(fp);
        #ifdef DEBUG
        sleep(1);
        #endif
        if(!num&&~buf)num++;
        if(buf=='\n')
        {
            num++;
        }
    }
    return num;
}

void split(char *src,const char *separator,char **dest,int *num) {
    char *p_next;
    int count = 0;
    if (src == NULL || strlen(src) == 0)
	    return;
    if (separator == NULL || strlen(separator) == 0)
        return;    
    p_next = strtok(src,separator);
    while(p_next != NULL) {
        *dest++ = p_next;
        ++count;
        p_next = strtok(NULL,separator);  
    }  
    *num = count;
}

char* getSubStr(char *s, regmatch_t *pmatch)
{
	char *buf = (char*)malloc(sizeof(char)*100);
	memset(buf, 0, sizeof(buf));
	memcpy(buf, s+pmatch->rm_so, pmatch->rm_eo - pmatch->rm_so);
	return buf;
}

int getAllWordList(char *word_buffer[],char buf[],char *pattern){
	regmatch_t pmatch;
	regex_t reg;
	int index = 0;
	// const char *pattern = "[a-z]+";		// 正则表达式
	// char buf[] = "HELLOsaiYear2012@gmail.com";	// 待搜索的字符串
 
	regcomp(&reg, pattern, REG_EXTENDED);	//编译正则表达式
	int offset = 0;
 	while(offset < strlen(buf))
	{
		int status = regexec(&reg, buf + offset, 1, &pmatch, 0);
		/* 匹配正则表达式，注意regexec()函数一次只能匹配一个，不能连续匹配，网上很多示例并没有说明这一点 */
		if(status == REG_NOMATCH)
        {
#ifdef DEBUG
			printf("No Match\n");
#endif
        }
		else if(pmatch.rm_so != -1)
		{
#ifdef DEBUG
			printf("Match:\n");
#endif
			char *p = getSubStr(buf + offset, &pmatch);
			word_buffer[index++] = p;
#ifdef DEBUG
 			printf("[%d, %d]: %s\n", offset + pmatch.rm_so + 1, offset + pmatch.rm_eo, p);
#endif
		}
		offset += pmatch.rm_eo;
	}
	regfree(&reg);		//释放正则表达式
	return index;
}

int countWithStopWord(FILE* stopword_file_pointer,FILE *input_file_pointer,char *seperator){
    int i = 0,j = 0;
    int stop_word_buffer_len = 0;
	int word_buffer_len = 0;
    
    char stop_word_buffer[BUFFER_SIZE] = "\0";
    char word_buffer[BUFFER_SIZE] = "\0";
    
    char *stop_word_list[MAX_WORD_NUMBER] = {NULL};
    char *word_list[MAX_WORD_NUMBER] = {NULL};
    
    int stop_word_number = 0;
    int word_number = 0;
    
    char line[MAX_LINE] = "\0";

	if(!stopword_file_pointer){
		printf("stop word file not found\n");
		exit(-1);
	}
	if(!input_file_pointer){
		printf("input file not found\n");
		exit(-1);
	}

	rewind(input_file_pointer);

    while(fgets(line,MAX_LINE,input_file_pointer) != NULL){
	   int len = strlen(line);
	   word_buffer_len += len;
	   memcpy(&word_buffer[strlen(word_buffer)],line,len);
    }

    while(fgets(line,MAX_LINE,stopword_file_pointer) != NULL){
	   int len = strlen(line);
	   stop_word_buffer_len += len;
	   memcpy(&stop_word_buffer[strlen(stop_word_buffer)],line,len);
    }

    word_number = getAllWordList(word_list,word_buffer,pattern);

    split(stop_word_buffer,seperator,stop_word_list,&stop_word_number);

    int count = 0;
    for (i = 0; i < word_number; ++i)
    {
    	count += 1;
    	for(j = 0;j<stop_word_number;j++){
    		if(!strcmp(stop_word_list[j],word_list[i])){
    			count -= 1;
    			break;
    		}
    	}
    }
    return count;
}
